package LeetCodeHot100TopInterview;

public class Q038_CountAndSay {
    //外观数列
    //每一项都是对前一项的报数
    //经典递归题
    public String countAndSay(int n) {
        //第一项是 1
        if (n == 1){
            return "1";
        }
        //拿到上一项的串
        String lastStr = countAndSay(n - 1);
        //走流程..
        char[] str = lastStr.toCharArray();
        StringBuilder sb = new StringBuilder();
        //time 个 x
        int time = 1;
        for (int i = 1; i < str.length; i++) {
            if (str[i] == str[i - 1]){
                time ++;
            }else {
                sb.append(time);
                sb.append(str[i - 1]);
                time = 1;
            }
        }
        //最后一位的结算
        sb.append(time);
        sb.append(str[str.length - 1]);

        return sb.toString();
    }
}
